package org.example.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.entity.Article;
import org.example.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    public List<Article> selectAll(Article article) {
        List<Article> list= articleMapper.selectAll(article);
        return list;
    }

    public Article selectById(Integer id) {
       Article article= articleMapper.selectById(id);
       return article;
    }

    public PageInfo<Article> selectPage(Article article, @RequestParam(defaultValue = "1") Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Article> list=articleMapper.selectAll(article);
        return PageInfo.of(list);
    }
    public void add(Article article) {
        article.setTime(DateUtil.now());
        articleMapper.insert(article);

    }

    public void delete(Integer id) {

        articleMapper.delete(id);
    }

    public void update(Article article) {
        articleMapper.update(article);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            this.delete(id);
        }
    }
}
